//
//  ViewController.m
//  UI29_Block
//
//  Created by dllo on 16/1/20.
//  Copyright © 2016年 dilo. All rights reserved.
//

#import "ViewController.h"
#import "SecondViewController.h"
#import "TouchView.h"
#import "NetworkingHandler.h"

/** 函数声明 */
int sum (int , int);

/** 函数实现 */

int sum (int x , int y) {
    
    return x + y;
    
}

int sub (int x, int y) {

    return  x - y;
}

@interface ViewController ()

@property (nonatomic, copy) void (^block) (NSString *);


@property (weak, nonatomic) IBOutlet UILabel *labelForBlock;

@property (weak, nonatomic) IBOutlet TouchView *toucheView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self functionReview];
    [self studyBlock];
    [self handleData];
    
    [self.toucheView viewDidClick:^(TouchView *view) {
       
        NSLog(@"点我就 crash");
        
    }];
    
    [self.toucheView buttonHandle:^(UIButton *deleteBtn) {
      
        
        NSLog(@"点击了删除按键");
        
    }];
    
    
    [self.toucheView editBtnHandle:^(UIButton *editBlock) {
        NSLog(@"点击了编辑按键吗");
    } deleBtnHandle:^(UIButton *deleBtn) {
       
        NSLog(@"点击了删除按键吗");
    }];
}
#pragma mark - 数据处理 (网络类使用 Block 回调)

- (void) handleData {

    NSString *str = @"http://api.map.baidu.com/place/v2/search?query=银行&region=大连&output=json&ak=6E823f587c95f0148c19993539b99295";
    
[NetworkingHandler networkingWithUrl:str completeHandle:^(NSURLResponse *response, NSData *data, id result) {

    
    
    NSLog(@"%@, %@, %@", response, data, result);
    
    
}];
 
    
}





#pragma mark - 复习函数

- (void)functionReview {

   /**
    * 函数由三部分:函数的声明,函数的实现,函数的调用
    */
    
    NSLog(@"%d", sum(3, 5));

    /**
     * 函数指针:指针指向一块内存,里面是一段代码段
     *
     */
    /** 定义一个函数指针变量 */
    int (*p) (int, int) = NULL;
    
    // 函数指针变量赋值赋值
    
    p = sum;
    p = sub;
    
    // 通过函数指针调用函数
    p(3, 5);
    NSLog(@"%d", p(3,5));

}

#pragma mark - block 
- (void)studyBlock {

    /**
     * 1.Block 和函数指针很相似.技巧: *p 换成 ^block
     * 2.Block 相当于匿名函数, 所以 block 变量赋值时,直接赋函数实现部分
     *
     */

    int (^block1)(int, int) = ^int(int x , int y) {
    
        return x + y;
    
    };
    
    // 调用 block
    
    block1 (3, 5);
    NSLog(@"%d", block1(3, 5));
    
    // block 语法
    
    // 无参无返
    void (^block2)(void) = ^(void) {
        NSLog(@"无参无返");
    
    };
    block2();
    
    //无参有返 block
    
    NSString* (^block3)(void) = ^NSString * (void) {
    
    return @"无参有返";
    
    };
    block3();
    NSLog(@"%@", block3());
    
    // 有参无返
    void (^block4)(NSString *, NSString *) = ^(NSString *str1, NSString *str2) {
    
        NSLog(@"%@", [str1 stringByAppendingString:str2]);
    
    };
    
    block4(@"da", @"lian");
    
    //有参有返
    
    NSString * (^block5) (NSString *, NSString *) = ^NSString *(NSString *str1, NSString *str2) {
    
        return [str1 stringByAppendingString:str2];
        
    };
    
    block5 (@"da", @"lian");
    NSLog(@"%@", block5(@"da", @"lian"));
    
    self.block = ^(NSString *str) {
    
        NSLog(@"%@", str);
    };
    
}

- (IBAction)handleBlock:(id)sender {
    
    // 调用 block
    self.block(@"aaa");
    
}


- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    
    // 获取第二界面的对象
    
   SecondViewController *second = [segue destinationViewController];
    
    second.blockOfSecond = ^(NSString *str) {
    
        self.labelForBlock.text = str;
    
    };
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
